package com.redoor.app.stake.auth.service.impl;

import com.redoor.app.stake.auth.commons.DatabaseServiceStatus;
import com.redoor.app.stake.auth.dao.BaseMenuMapper;
import com.redoor.app.stake.auth.dao.BaseRoleMenuMapper;
import com.redoor.app.stake.auth.model.BaseMenu;
import com.redoor.app.stake.auth.model.BaseMenuCriteria;
import com.redoor.app.stake.auth.model.BaseRoleMenu;
import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.model.constants.ManagerRedisConstants;
import com.redoor.app.stake.auth.model.serviceModel.BaseMenuServiceBean;
import com.redoor.app.stake.auth.service.BaseMenuRedisService;
import com.redoor.app.stake.auth.service.BaseMenuService;

import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.common.service.RedisService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * Created by wangjunlei on 2018-02-07 15:19:13.
 */
@Service(value = "BaseMenuService")
public class BaseMenuServiceImpl extends AbstractService<BaseMenu, String> implements BaseMenuService {

    @Autowired
    private BaseMenuMapper baseMenuMapper;
    @Autowired
    private BaseRoleMenuMapper baseRoleMenuMapper;

    @Autowired
    private BaseMenuRedisService baseMenuRedisService;

    @Autowired
    private BaseUserService baseUserService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public int updateByPrimaryKey(BaseMenuCriteria baseMenuCriteria) {
        BaseMenu baseMenu = new BaseMenu();
        baseMenu.setUuid(baseMenuCriteria.getUuid());
        baseMenu.setParentuuid(baseMenuCriteria.getParentuuid());
        baseMenu.setName(baseMenuCriteria.getMenuName());
        baseMenu.setUrl(baseMenuCriteria.getUrl());
        baseMenu.setIcon(baseMenuCriteria.getIcon());
        baseMenu.setOrdernum(baseMenuCriteria.getOrderNum());
        baseMenu.setPerms(baseMenuCriteria.getPerms());
        baseMenu.setType(baseMenuCriteria.getType());
        baseMenu.setUuid(baseMenuCriteria.getUuid());
        baseMenu.setOrdernum(baseMenuCriteria.getOrderNum());
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        baseMenu.setUpdateruuid(loginUser.getUuid());
        baseMenu.setUpdatedat(new Date());
        int status = this.baseMenuMapper.updateByPrimaryKeySelective(baseMenu);
        if (baseMenu.getType() == 2){
            this.insertButtnAllToRedis();
        }
        return status;
    }

    @Override
    public List<BaseMenuServiceBean> qryMenu(BaseMenuCriteria baseMenuCriteria) {
        return this.baseMenuMapper.qryMenu(baseMenuCriteria);
    }

    @Override
    public List<BaseMenuServiceBean> qryMenuUserUuid(String userUuid) {

        //一级菜单查询
        List<BaseMenuServiceBean> list = this.baseMenuMapper.qryMenuUserUuid(userUuid);
            for (BaseMenuServiceBean action : list){
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("roleUuid",action.getRoleUuid());
                map.put("userUuid",action.getUuid());
                action.setChildMenu(this.baseMenuMapper.qryMenuByParentUuid(map));
                for (BaseMenuServiceBean childMenu : action.getChildMenu()) {
                    Map<String, Object> map1 = new HashMap<String, Object>();
                    map1.put("roleUuid",childMenu.getRoleUuid());
                    map1.put("userUuid",childMenu.getUuid());
                    childMenu.setGrandson(this.baseMenuMapper.qryButtnByParentUuid(map1));
                }
            }
        return list;
    }

    @Override
    public List<BaseMenu> qryMenuTree(String uuid) {
        return  this.baseMenuMapper.qryMenuAll(uuid);
    }

    @Override
    public List<BaseMenu> qryPrimaryMenu(String type) {
        return this.baseMenuMapper.qryMenuList(type);
    }

    @Override
    public void deleteMenu(String uuid) {
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        BaseMenu trem = new BaseMenu();
        trem.setUuid(uuid);
        trem.setUpdateruuid(loginUser.getUuid());
        trem.setUpdatedat(new Date());
        trem.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_DEL_STATUS.getCode()));
        this.baseMenuMapper.updateByPrimaryKeySelective(trem);
        BaseRoleMenu baseRoleMenu = new BaseRoleMenu();
        baseRoleMenu.setMenuuuid(trem.getUuid());
        //删除中间表信息
        this.baseRoleMenuMapper.delete(baseRoleMenu);
        //更新Redis按钮列表
        this.insertButtnAllToRedis();
    }

    @Override
    public List<BaseMenu> qryMenuAll() {
        return  this.baseMenuMapper.qryMenuAll(null);
    }

    @Override
    public Integer qryMenuCountByName(String name) {
        Condition condition = new Condition(BaseMenu.class);
        condition.createCriteria().andCondition("`status`=1000 AND type <> 2 AND `name` = '" + name + "'");
        return this.baseMenuMapper.selectCountByCondition(condition);
    }

    @Override
    public List<String> qryMenuPermByUseruuid(String uuid) {
        return this.baseMenuMapper.qryMenuPermByUseruuid(uuid);
    }

    @Override
    public void insertButtnAllToRedis() {
        //删除清空原有数据
        this.redisUtils.remove(ManagerRedisConstants.REDIS_BUTTEN);
        //新增数据
        this.baseMenuRedisService.insertMenuToRedis(this.qryButtenAll());
    }

    @Override
    public void insertMenu(BaseMenuCriteria baseMenuCriteria) {
        BaseMenu baseMenu = new BaseMenu();
        baseMenu.setParentuuid(baseMenuCriteria.getParentuuid());
        baseMenu.setName(baseMenuCriteria.getMenuName());
        baseMenu.setUrl(baseMenuCriteria.getUrl());
        baseMenu.setIcon(baseMenuCriteria.getIcon());
        baseMenu.setPerms(baseMenuCriteria.getPerms());
        baseMenu.setType(baseMenuCriteria.getType());
        baseMenu.setOrdernum(baseMenuCriteria.getOrderNum());
        baseMenu.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        baseMenu.setUuid(UUID.randomUUID().toString());
        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        baseMenu.setCreatoruuid(loginUser.getUuid());
        baseMenu.setCreatedat(new Date());
        this.baseMenuMapper.insert(baseMenu);
        if(baseMenu.getType() == 2){
            this.insertButtnAllToRedis();
        }
    }

    /**
     * 查询所有按钮菜单
     * @Author zhangYiHang
     * @Date 2018/4/13 10:10
     * @Param
     */
    private List<BaseMenu> qryButtenAll(){
        BaseMenu baseMenu = new BaseMenu();
        baseMenu.setType(2);
        baseMenu.setStatus(Integer.valueOf(DatabaseServiceStatus.SERVICE_NORMAL_STATUS.getCode()));
        return this.baseMenuMapper.select(baseMenu);
    }
}
